import Vue from 'vue'
import Vuex from 'vuex'
import router from '@/router/index'
import { isWechat, wxAuth } from '@/libs/utils'
import account from './modules/account'
import user from './modules/user'
import { removeAccessTokenStorage, removeRefreshTokenStorage, removeUserInfoStorage } from '@/libs/storage'
Vue.use(Vuex)

const state = {}
const getters = {}
const mutations = {}
const actions = {
  // 登出操作
  logout ({ commit }) {
    // console.log('登出')
    commit('setToken', {})
    commit('setUserInfo', {})
    removeAccessTokenStorage()
    removeRefreshTokenStorage()
    removeUserInfoStorage()
    if (isWechat()) {
      wxAuth('/home')
    } else {
      router.push({ name: 'login' })
    }
  }
}

const store = new Vuex.Store({
  modules: {
    account,
    user
  },
  state,
  getters,
  mutations,
  actions
})

export default store
